package com.example.libraryManager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.libraryManager.entity.Books;
import com.example.libraryManager.form.SearchForm;
import com.example.libraryManager.mapper.BooksMapper;
import com.example.libraryManager.service.IBooksService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.libraryManager.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author admin
 * @since 2022-09-02
 */
@Service
public class BooksService extends ServiceImpl<BooksMapper, Books> implements IBooksService {

    @Autowired
    private BooksMapper booksMapper;

    /**
     * 分页查询
     * @param size
     * @param page
     * @return
     */
    @Override
    public PageVo pageList(Integer page,Integer size) {
        Page<Books> booksPage=new Page<>(page,size);

        Page<Books> resultPage=this.booksMapper.selectPage(booksPage,null);

        PageVo pageVo=new PageVo();
        pageVo.setData(resultPage.getRecords());
        pageVo.setTotal(resultPage.getTotal());
        return pageVo;
    }

    /**
     * 搜索 + 分页
     * @param searchForm
     * @return
     */
    @Override
    public PageVo search(SearchForm searchForm) {
        Page<Books> booksPage = new Page<>(searchForm.getPage(), searchForm.getSize());

        //模糊查询加分页
        Page<Books> resultPage = null;

        //判断value值是null 而且 key值是null
        if (searchForm.getValue().equals("")&& !searchForm.getKey().equals("")) {

            QueryWrapper<Books> wrapper=new QueryWrapper<>();
            wrapper.eq("kind_id",searchForm.getKey());
            resultPage = this.booksMapper.selectPage(booksPage, wrapper);

            //判断value值不是null 而且 key值是null
        } else if (!searchForm.getValue().equals("") && searchForm.getKey().equals("")){

            QueryWrapper<Books> wrapper=new QueryWrapper<>();
            wrapper.eq("book_name",searchForm.getValue());
            resultPage = this.booksMapper.selectPage(booksPage, wrapper);

            //判断value值不是null 而且 key值不是null
        } else {

            QueryWrapper<Books> wrapper=new QueryWrapper<>();
            wrapper.eq("book_name",searchForm.getValue()).eq("kind_id",searchForm.getKey());
            resultPage=this.booksMapper.selectPage(booksPage,wrapper);

        }

        PageVo pageVo=new PageVo();
        pageVo.setData(resultPage.getRecords());
        pageVo.setTotal(resultPage.getTotal());

        return pageVo;
    }
}
